package com.fishbone.cloud.user;

import com.fishbone.swagger.annotation.EnableFishBoneSwagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huyaxi
 */
@EnableOAuth2Sso
@EnableFishBoneSwagger
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class FishBoneUserApplication extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(FishBoneUserApplication.class,args);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(passResources()).permitAll()
                .anyRequest().authenticated();
    }

    /**
     * 资源白名单
     * @return
     */
    private String[] passResources() {
        List<String> resources = new ArrayList<>();
        resources.add("/user/user-auth");
        String[] paths = new String[resources.size()];
        return resources.toArray(paths);
    }
}
